﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Com.FastSearch.SharePoint.WebControls.ESPSearchWebParts.UI;
using Microsoft.SharePoint.Portal.Audience.AdminUI;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ESPWebPartsUnitTest.src
{
    /// <summary>
    /// Summary description for ESPSearchActionTest
    /// </summary>
    [TestClass]
    public class ESPSearchActionTest
    {
        public ESPSearchActionTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestBuildListFromString()
        {
            string testString = "default:0:DEFAULT:True;size:1:SIZE:False;";
            List <SearchSortAction> searchActions = ESPSearchAction.BuildListFromString(testString);
            Assert.AreEqual(2, searchActions.Count);
            AssertSearchAction(searchActions[0], "default", "DEFAULT", SortOrderEnum.Ascending, true);
            AssertSearchAction(searchActions[1], "size", "SIZE", SortOrderEnum.Descending, false);
        }

        [TestMethod]
        public void TestBuildListFromStringWrongInput()
        {
            string testString = "default:0:DEFAULT:True;size";
            List<SearchSortAction> searchActions = ESPSearchAction.BuildListFromString(testString);
            Assert.AreEqual(1, searchActions.Count);
            AssertSearchAction(searchActions[0], "default", "DEFAULT", SortOrderEnum.Ascending, true);            
        }

        [TestMethod]
        public void TestBuildListFromStringSomeEmptyFields()
        {
            string testString = "default:0::;size:::;";
            List<SearchSortAction> searchActions = ESPSearchAction.BuildListFromString(testString);
            Assert.AreEqual(2, searchActions.Count);
            AssertSearchAction(searchActions[0], "default", "default", SortOrderEnum.Ascending, true);
            AssertSearchAction(searchActions[1], "size", "size", SortOrderEnum.Ascending, true);
        }

        [TestMethod]
        public void TestBuildStringFromList()
        {
            List<SearchSortAction> searchActions =  new List<SearchSortAction>();
            searchActions.Add(CreateSearchSortAction("default", "DEFAULT", SortOrderEnum.Ascending, true));
            searchActions.Add(CreateSearchSortAction("size", "SIZE", SortOrderEnum.Descending, false));
            searchActions.Add(CreateSearchSortAction("somefield", null, SortOrderEnum.Ascending, false));
            string searchActionsString = ESPSearchAction.BuildStringFromList(searchActions);
            Assert.AreEqual("default:0:DEFAULT:True;size:1:SIZE:False;somefield:0:somefield:False;", searchActionsString);           
        }

        private SearchSortAction CreateSearchSortAction(String field, string displayName, SortOrderEnum defaultSortOrder, bool isRankProfile)
        {
            SearchSortAction ssaction = new SearchSortAction(field);
            ssaction.DefaultSortOrder = defaultSortOrder;
            if (displayName != null)
            {
                ssaction.DisplayName = displayName;    
            }
            ssaction.IsRankProfile = isRankProfile;

            return ssaction;
        }

        private void AssertSearchAction(SearchSortAction ssaction, String field, string displayName, SortOrderEnum defaultSortOrder, bool isRankProfile)
        {            
            Assert.AreEqual(displayName, ssaction.DisplayName);
            Assert.AreEqual(field, ssaction.FieldName);
            Assert.AreEqual(defaultSortOrder, ssaction.DefaultSortOrder);
            Assert.AreEqual(isRankProfile, ssaction.IsRankProfile);
        }
    }
}
